#include "messagebox.h"
#include "ui_messagebox.h"
#include <QDateTime>

messagebox::messagebox(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::messagebox)
{
    m_ui->setupUi(this);
    this->setAttribute(Qt::WA_QuitOnClose, false);
    closetimer = new QTimer(this);
    closetimer->setInterval(1000);
    seconds = 0;
    connect(closetimer, SIGNAL(timeout()), this, SLOT(timerFunc()));
    connect(m_ui->pushButtonClose, SIGNAL(clicked()), this, SLOT(close()));
}

messagebox::~messagebox()
{
    delete m_ui;
    delete closetimer;
}

void messagebox::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void messagebox::showMessageBox(const QString &message, int showTime, int sendTime)
{
    m_ui->textBrowser->setPlainText(message);
    m_ui->label_sendtime->setText(QDateTime::fromTime_t(sendTime).toString("dd.MM.yyyy hh:mm:ss"));
    m_ui->label_receivetime->setText(QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss"));

    if (showTime>0)
    {
        seconds = showTime;
        m_ui->pushButtonClose->setText(tr("Close ")+QString::number(seconds, 10));
        closetimer->start();
    }

    this->show();
}

void messagebox::timerFunc()
{
    seconds--;
    if (seconds<=0)
    {
        this->close();
    }
    m_ui->pushButtonClose->setText(tr("Close ")+QString::number(seconds, 10));
}

void messagebox::closeEvent(QCloseEvent *event)
{
    closetimer->stop();
}
